package com.lmiot.xyclick.Share;

import android.app.Dialog;
import android.content.Intent;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.os.Environment;
import android.support.v4.app.FragmentActivity;
import android.text.TextUtils;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.TextView;
import com.lmiot.xyclick.Util.SpFlag;
import com.bumptech.glide.Glide;
import com.lmiot.xyclick.Activity.BaseActivity;
import com.lmiot.xyclick.Activity.WebViewActivity;
import com.lmiot.xyclick.App.MyApp;
import com.lmiot.xyclick.Bean.SQL.ActionBeanSqlUtil;
import com.lmiot.xyclick.Bean.SQL.AutoBean;
import com.lmiot.xyclick.Bean.SQL.AutoBeanSqlUtil;
import com.lkx.liandianqinew.R;
import com.lmiot.xyclick.Util.ActivityUtil;
import com.lmiot.xyclick.Util.ClickUtils;
import com.lmiot.xyclick.Util.DataUtil;
import com.lmiot.xyclick.Util.DpUtil;
import com.lmiot.xyclick.Util.FileUtils;
import com.lmiot.xyclick.Util.LayoutDialogUtil;
import com.lmiot.xyclick.Util.LogUtil;
import com.lmiot.xyclick.Util.MathUtils;
import com.lmiot.xyclick.Util.PhoneUtil;
import com.lmiot.xyclick.inteface.OnBasicListener;
import com.xiaoyi.intentsdklibrary.Bean.ShareBean;
import com.xiaoyi.intentsdklibrary.Bean.ShareBeanSqlUtil;
import com.youyi.yyviewsdklibrary.Dialog.interfaces.OnSelectListener;
import com.youyi.yyviewsdklibrary.Dialog.util.ToastUtil;
import com.youyi.yyviewsdklibrary.View.TitleBarView;
import com.youyi.yyviewsdklibrary.View.roundedimageview.RoundedImageView;
import com.youyi.yyviewsdklibrary.YYSDK;
import java.io.File;
import java.util.ArrayList;
import java.util.List;

public class UploadShareActivity extends BaseActivity implements View.OnClickListener {
    private AutoBean mAutoBean;
    private String mAutoID;
    RoundedImageView mAutoIcon;
    ImageView mCbBg;
    private AutoBean mChoseAutoBean;
    private Dialog mDialog;
    TextView mIdChange;
    TextView mIdChangeType;
    EditText mIdEditDetail;
    EditText mIdEditFen;
    EditText mIdEditName;
    EditText mIdEditPassword;
    TextView mIdEmpty;
    ImageView mIdImg;
    TextView mIdName;
    TextView mIdShareType;
    TextView mIdTime;
    TitleBarView mIdTitleBar;
    TextView mIdUpload;
    private Intent mIntent;
    private String mShareID;
    private List<String> mTitleType;
    private String mTypeNum = "0";

    /* access modifiers changed from: protected */
    @Override // android.support.v4.app.SupportActivity, com.lmiot.xyclick.Activity.BaseActivity, android.support.v7.app.AppCompatActivity, android.support.v4.app.FragmentActivity
    public void onCreate(Bundle bundle) {
        super.onCreate(bundle);
        setContentView(R.layout.activity_share_upload);
        initView();
        setTitle();
        initData();
    }

    private void initData() {
        ShareBean searchByID;
        this.mAutoID = getIntent().getStringExtra("autoID");
        this.mShareID = getIntent().getStringExtra("shareID");
        if (TextUtils.isEmpty(this.mAutoID)) {
            List<AutoBean> searchAll = AutoBeanSqlUtil.getInstance().searchAll();
            if (searchAll.size() > 0) {
                this.mAutoBean = searchAll.get(0);
                this.mIdEmpty.setVisibility(View.GONE);
            } else {
                this.mIdEmpty.setVisibility(View.VISIBLE);
            }
        } else {
            this.mAutoBean = AutoBeanSqlUtil.getInstance().searchByID(this.mAutoID);
        }
        if (!TextUtils.isEmpty(this.mShareID) && (searchByID = ShareBeanSqlUtil.getInstance().searchByID(this.mShareID)) != null) {
            this.mIdEditName.setText(searchByID.getScript_name());
            String user_scene = searchByID.getUser_scene();
            this.mIdEditPassword.setText(TextUtils.isEmpty(user_scene) ? "" : FileUtils.decodeString(user_scene));
            this.mIdEditDetail.setText(searchByID.getScript_note());
            int need_acc_point = searchByID.getNeed_acc_point();
            EditText editText = this.mIdEditFen;
            editText.setText(need_acc_point + "");
            this.mTypeNum = searchByID.getScript_type_code();
            this.mIdShareType.setText(ShareSDK.getInstance().getTypeName(this.mTypeNum));
            this.mAutoBean = AutoBeanSqlUtil.getInstance().searchByID(searchByID.getScript_type_id());
        }
    }

    @Override // com.lmiot.xyclick.Activity.BaseActivity, android.support.v4.app.FragmentActivity
    public void onResume() {
        super.onResume();
        AutoBean autoBean = this.mAutoBean;
        if (autoBean != null) {
            String autoType = autoBean.getAutoType();
            boolean isEnable = this.mAutoBean.getIsEnable();
            File file = new File(Environment.getExternalStorageDirectory() + "/XYClick/temp", this.mAutoBean.getAutoID() + ".jpg");
            if (file.exists()) {
                this.mAutoIcon.setImageBitmap(BitmapFactory.decodeFile(file.getAbsolutePath()));
                this.mCbBg.setVisibility(View.GONE);
                this.mIdImg.setVisibility(View.GONE);
                this.mAutoIcon.setVisibility(View.VISIBLE);
            } else {
                this.mCbBg.setVisibility(View.VISIBLE);
                this.mIdImg.setVisibility(View.VISIBLE);
                this.mAutoIcon.setVisibility(View.GONE);
                Glide.with((FragmentActivity) this).load(Integer.valueOf((int) R.drawable.play_auto_blue)).into(this.mAutoIcon);
            }
            if (TextUtils.isEmpty(autoType)) {
                autoType = "click";
            }
            String autoName = this.mAutoBean.getAutoName();
            this.mIdTime.setText(getString(R.string.cr_t) + this.mAutoBean.getCreateTime());
            this.mIdName.setText(autoName);
            if (autoType.equals("click")) {
                Glide.with((FragmentActivity) this).load(Integer.valueOf((int) R.drawable.play_auto)).into(this.mIdImg);
            } else if (autoType.equals("time") || autoType.equals("notic") || autoType.equals("screen") || autoType.equals("notic_now") || autoType.equals("screen_now")) {
                if (autoType.equals("time")) {
                    Glide.with((FragmentActivity) this).load(Integer.valueOf(isEnable ? R.drawable.auto_time : R.drawable.auto_time_stop)).into(this.mIdImg);
                } else if (autoType.equals("notic") || autoType.equals("notic_now")) {
                    Glide.with((FragmentActivity) this).load(Integer.valueOf(isEnable ? R.drawable.auto_type_msg : R.drawable.auto_type_msg_stop)).into(this.mIdImg);
                } else if (autoType.equals("screen") || autoType.equals("screen_now")) {
                    Glide.with((FragmentActivity) this).load(Integer.valueOf(isEnable ? R.drawable.auto_type_screen : R.drawable.auto_type_screen_stop)).into(this.mIdImg);
                }
            }
            this.mIdEditName.setText(this.mAutoBean.getAutoName());
        }
    }

    private void setTitle() {
        this.mIdTitleBar.setOnItemClickListener(new TitleBarView.onItemClickListener() {
            /* class com.lmiot.xyclick.Share.UploadShareActivity.AnonymousClass1 */

            @Override // com.youyi.yyviewsdklibrary.View.TitleBarView.onItemClickListener
            public void onTitleClick(View view) {
            }

            @Override // com.youyi.yyviewsdklibrary.View.TitleBarView.onItemClickListener
            public void onBackClick(View view) {
                UploadShareActivity.this.finish();
            }

            @Override // com.youyi.yyviewsdklibrary.View.TitleBarView.onItemClickListener
            public void onMenuClick(View view) {
                UploadShareActivity.this.mIntent = new Intent(UploadShareActivity.this, WebViewActivity.class);
                UploadShareActivity.this.mIntent.putExtra(SpFlag.k, "《共享须知》");
                UploadShareActivity.this.mIntent.putExtra("url", "https://ai.xiaoyizhineng.com:441/download/HelpVideo/autoclick/know.html");
                UploadShareActivity uploadShareActivity = UploadShareActivity.this;
                uploadShareActivity.startActivity(uploadShareActivity.mIntent);
            }
        });
    }

    private void chosAutoDialog() {
        Dialog dialog = this.mDialog;
        if (dialog != null && dialog.isShowing()) {
            this.mDialog.dismiss();
        }
        Dialog createDailog = LayoutDialogUtil.createDailog(this, R.layout.dialog_chose_autobean);
        this.mDialog = createDailog;
        ListView listView = (ListView) createDailog.findViewById(R.id.id_listivew);
        List<AutoBean> searchAll = AutoBeanSqlUtil.getInstance().searchAll();
        if (searchAll.size() > 5) {
            listView.setLayoutParams(new LinearLayout.LayoutParams(-1, DpUtil.dip2px(this, 300.0f)));
        } else {
            listView.setLayoutParams(new LinearLayout.LayoutParams(-1, -2));
        }
        this.mChoseAutoBean = this.mAutoBean;
        listView.setAdapter((ListAdapter) new AutoAdapter(searchAll));
        ((TextView) this.mDialog.findViewById(R.id.tv_sure)).setOnClickListener(new View.OnClickListener() {
            /* class com.lmiot.xyclick.Share.UploadShareActivity.AnonymousClass2 */

            public void onClick(View view) {
                UploadShareActivity.this.mDialog.dismiss();
                UploadShareActivity uploadShareActivity = UploadShareActivity.this;
                uploadShareActivity.mAutoBean = uploadShareActivity.mChoseAutoBean;
                UploadShareActivity.this.onResume();
            }
        });
        ((TextView) this.mDialog.findViewById(R.id.tv_cancel)).setOnClickListener(new View.OnClickListener() {
            /* class com.lmiot.xyclick.Share.UploadShareActivity.AnonymousClass3 */

            public void onClick(View view) {
                UploadShareActivity.this.mDialog.dismiss();
            }
        });
    }

    private void initView() {
        this.mIdTitleBar = (TitleBarView) findViewById(R.id.id_title_bar);
        this.mIdEmpty = (TextView) findViewById(R.id.id_empty);
        this.mCbBg = (ImageView) findViewById(R.id.cb_bg);
        this.mIdImg = (ImageView) findViewById(R.id.id_img);
        this.mAutoIcon = (RoundedImageView) findViewById(R.id.auto_icon);
        this.mIdName = (TextView) findViewById(R.id.id_name);
        this.mIdTime = (TextView) findViewById(R.id.id_time);
        this.mIdChange = (TextView) findViewById(R.id.id_change);
        this.mIdEditName = (EditText) findViewById(R.id.id_edit_name);
        this.mIdShareType = (TextView) findViewById(R.id.id_share_type);
        this.mIdChangeType = (TextView) findViewById(R.id.id_change_type);
        this.mIdEditFen = (EditText) findViewById(R.id.id_edit_fen);
        this.mIdEditPassword = (EditText) findViewById(R.id.id_edit_password);
        this.mIdEditDetail = (EditText) findViewById(R.id.id_edit_detail);
        this.mIdUpload = (TextView) findViewById(R.id.id_upload);
        this.mIdChange.setOnClickListener(this);
        this.mIdChangeType.setOnClickListener(this);
        this.mIdUpload.setOnClickListener(this);
    }

    public void onClick(View view) {
        int id = view.getId();
        if (id == R.id.id_change) {
            chosAutoDialog();
        } else if (id == R.id.id_change_type) {
            ArrayList arrayList = new ArrayList();
            this.mTitleType = new ArrayList();
            ShareTypeEnum[] values = ShareTypeEnum.values();
            for (ShareTypeEnum shareTypeEnum : values) {
                if (!shareTypeEnum.equals(ShareTypeEnum.top)) {
                    arrayList.add(shareTypeEnum.getName());
                    this.mTitleType.add(shareTypeEnum.getTypeCode());
                }
            }
            YYSDK.getInstance().showPopup(this, (String[]) arrayList.toArray(new String[arrayList.size()]), null, view, new OnSelectListener() {
                /* class com.lmiot.xyclick.Share.UploadShareActivity.AnonymousClass4 */

                @Override // com.youyi.yyviewsdklibrary.Dialog.interfaces.OnSelectListener
                public void onSelect(int i, String str) {
                    UploadShareActivity uploadShareActivity = UploadShareActivity.this;
                    uploadShareActivity.mTypeNum = (String) uploadShareActivity.mTitleType.get(i);
                    UploadShareActivity.this.mIdShareType.setText(ShareSDK.getInstance().getTypeName(UploadShareActivity.this.mTypeNum));
                }
            });
        } else if (id == R.id.id_upload) {
            upLoad();
        }
    }

    /* access modifiers changed from: private */
    public class AutoAdapter extends BaseAdapter {
        List<AutoBean> list;

        public Object getItem(int i) {
            return null;
        }

        public long getItemId(int i) {
            return 0;
        }

        public AutoAdapter(List<AutoBean> list2) {
            this.list = list2;
        }

        public int getCount() {
            return this.list.size();
        }

        public View getView(int i, View view, ViewGroup viewGroup) {
            View inflate = View.inflate(UploadShareActivity.this, R.layout.item_chose_auto, null);
            ImageView imageView = (ImageView) inflate.findViewById(R.id.id_checked);
            ImageView imageView2 = (ImageView) inflate.findViewById(R.id.id_unchecked);
            ImageView imageView3 = (ImageView) inflate.findViewById(R.id.cb_bg);
            ImageView imageView4 = (ImageView) inflate.findViewById(R.id.id_img);
            RoundedImageView roundedImageView = (RoundedImageView) inflate.findViewById(R.id.auto_icon);
            final AutoBean autoBean = this.list.get(i);
            ((TextView) inflate.findViewById(R.id.id_name)).setText(autoBean.getAutoName());
            if (autoBean.getAutoID().equals(UploadShareActivity.this.mChoseAutoBean.getAutoID())) {
                imageView.setVisibility(View.VISIBLE);
                imageView2.setVisibility(View.GONE);
            } else {
                imageView.setVisibility(View.GONE);
                imageView2.setVisibility(View.VISIBLE);
            }
            String autoType = autoBean.getAutoType();
            boolean isEnable = autoBean.getIsEnable();
            File file = new File(Environment.getExternalStorageDirectory() + "/XYClick/temp", autoBean.getAutoID() + ".jpg");
            if (file.exists()) {
                roundedImageView.setImageBitmap(BitmapFactory.decodeFile(file.getAbsolutePath()));
                imageView3.setVisibility(View.GONE);
                imageView4.setVisibility(View.GONE);
                roundedImageView.setVisibility(View.VISIBLE);
            } else {
                imageView3.setVisibility(View.VISIBLE);
                imageView4.setVisibility(View.VISIBLE);
                roundedImageView.setVisibility(View.GONE);
                Glide.with((FragmentActivity) UploadShareActivity.this).load(Integer.valueOf((int) R.drawable.play_auto_blue)).into(roundedImageView);
            }
            if (autoType.equals("click")) {
                Glide.with((FragmentActivity) UploadShareActivity.this).load(Integer.valueOf((int) R.drawable.play_auto)).into(imageView4);
            } else if (autoType.equals("time") || autoType.equals("notic") || autoType.equals("screen") || autoType.equals("notic_now") || autoType.equals("screen_now")) {
                if (autoType.equals("time")) {
                    Glide.with((FragmentActivity) UploadShareActivity.this).load(Integer.valueOf(isEnable ? R.drawable.auto_time : R.drawable.auto_time_stop)).into(imageView4);
                } else if (autoType.equals("notic") || autoType.equals("notic_now")) {
                    Glide.with((FragmentActivity) UploadShareActivity.this).load(Integer.valueOf(isEnable ? R.drawable.auto_type_msg : R.drawable.auto_type_msg_stop)).into(imageView4);
                } else if (autoType.equals("screen") || autoType.equals("screen_now")) {
                    Glide.with((FragmentActivity) UploadShareActivity.this).load(Integer.valueOf(isEnable ? R.drawable.auto_type_screen : R.drawable.auto_type_screen_stop)).into(imageView4);
                }
            }
            inflate.setOnClickListener(new View.OnClickListener() {
                /* class com.lmiot.xyclick.Share.UploadShareActivity.AutoAdapter.AnonymousClass1 */

                public void onClick(View view) {
                    UploadShareActivity.this.mChoseAutoBean = autoBean;
                    AutoAdapter.this.notifyDataSetChanged();
                }
            });
            return inflate;
        }
    }

    private void upLoad() {
        try {
            if (TextUtils.isEmpty(this.mIdEditName.getText().toString())) {
                ToastUtil.warning("名称不能为空！");
            } else if (TextUtils.isEmpty(this.mIdEditDetail.getText().toString())) {
                ToastUtil.warning("描述不能为空！");
            } else if (this.mAutoBean == null) {
                ToastUtil.warning("请先选择一个自动化！");
            } else {
                final int fen = getFen();
                if (fen > 200) {
                    ToastUtil.warning("积分不能超过200分！");
                    return;
                }
                FileUtils.saveToLocalData(this.mAutoBean.getAutoID(), new OnBasicListener() {
                    /* class com.lmiot.xyclick.Share.UploadShareActivity.AnonymousClass5 */

                    @Override // com.lmiot.xyclick.inteface.OnBasicListener
                    public void result(boolean z, String str) {
                    }
                });
                File file = new File(Environment.getExternalStorageDirectory() + "/XYClick/data", this.mAutoBean.getAutoID() + "." + DataUtil.FILE_XYCLICK_DATA);
                final String fileToBase64String = FileUtils.fileToBase64String(file.getAbsolutePath());
                String trim = this.mIdEditPassword.getText().toString().trim();
                final String trim2 = this.mIdEditName.getText().toString().trim();
                final String imei = PhoneUtil.getIMEI(MyApp.getContext());
                final String trim3 = this.mIdEditDetail.getText().toString().trim();
                final String fileSize = FileUtils.getFileSize(file);
                final String encode = TextUtils.isEmpty(trim) ? "" : FileUtils.encode(trim);
                final int size = ActionBeanSqlUtil.getInstance().searchByID(this.mAutoBean.getAutoID()).size();
                final String str = PhoneUtil.getBrand() + "#" + PhoneUtil.getModel() + "#" + MyApp.mWidth + "#" + MyApp.mHeight;
                final String packageName = MyApp.getContext().getPackageName();
                final String aPPVersion = PhoneUtil.getAPPVersion();
                final String str2 = this.mTypeNum;
                final String autoID = this.mAutoBean.getAutoID();
                LogUtil.d("UploadShareActivity", "fileTtypeCode:" + str2 + ":" + this.mShareID);
                if (TextUtils.isEmpty(this.mShareID)) {
                    ShareSDK.getInstance().checkMyNum(PhoneUtil.getIMEI(MyApp.getContext()), new OnBasicListener() {
                        /* class com.lmiot.xyclick.Share.UploadShareActivity.AnonymousClass6 */

                        @Override // com.lmiot.xyclick.inteface.OnBasicListener
                        public void result(boolean z, String str) {
                            if (z) {
                                ShareSDK instance = ShareSDK.getInstance();
                                String str3 = autoID;
                                String str4 = trim2;
                                String str5 = imei;
                                String str6 = trim3;
                                String str7 = fileToBase64String;
                                String str8 = fileSize;
                                String str9 = encode;
                                instance.upLoadFile(str2, str3, str4, str5, "", str6, str7, str8, str9, size, str9, str, packageName, aPPVersion, false, fen, new OnBasicListener() {
                                    /* class com.lmiot.xyclick.Share.UploadShareActivity.AnonymousClass6.AnonymousClass1 */

                                    @Override // com.lmiot.xyclick.inteface.OnBasicListener
                                    public void result(boolean z, String str) {
                                        if (z) {
                                            ClickUtils.onlyVibrate(MyApp.getContext());
                                            ToastUtil.success("上传成功！");
                                            UploadShareActivity.this.finish();
                                            return;
                                        }
                                        ToastUtil.err("上传失败！");
                                    }
                                });
                                return;
                            }
                            LayoutDialogUtil.showSureDialog(UploadShareActivity.this, "超出10个自动化", "为保障质量上传的质量;\n每个ID最多只能上传10个自动化;\n请先删除部分自动化后再重新上传，谢谢~", true, false, "返回", "确定", new LayoutDialogUtil.OnResultClickListener() {
                                /* class com.lmiot.xyclick.Share.UploadShareActivity.AnonymousClass6.AnonymousClass2 */

                                @Override // com.lmiot.xyclick.Util.LayoutDialogUtil.OnResultClickListener
                                public void result(boolean z) {
                                    if (z) {
                                        ActivityUtil.skipActivity(UploadShareActivity.this, ShareSelfActivity.class);
                                    }
                                }
                            });
                        }
                    });
                } else {
                    ShareSDK.getInstance().editFile(this.mShareID, str2, autoID, trim2, imei, "", trim3, fileToBase64String, fileSize, encode, size, encode, str, packageName, aPPVersion, false, fen, new OnBasicListener() {
                        /* class com.lmiot.xyclick.Share.UploadShareActivity.AnonymousClass7 */

                        @Override // com.lmiot.xyclick.inteface.OnBasicListener
                        public void result(boolean z, String str) {
                            if (z) {
                                ClickUtils.onlyVibrate(MyApp.getContext());
                                ToastUtil.success("上传成功！");
                                UploadShareActivity.this.finish();
                                return;
                            }
                            ToastUtil.err("上传失败！");
                        }
                    });
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
            ToastUtil.err("读取数据错误！");
        }
    }

    private int getFen() {
        try {
            return MathUtils.parseInt(this.mIdEditFen.getText().toString().trim());
        } catch (Exception unused) {
            return 0;
        }
    }
}
